package ynu.edu.model.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ynu.edu.common.domain.R;
import ynu.edu.model.entity.ModelConfig;
import ynu.edu.model.service.ModelConfigService;

import java.util.List;

/**
 * 模型配置控制器
 */
@Slf4j
@RestController
@RequestMapping("/modelconfig")
@RequiredArgsConstructor
@Api(tags = "模型配置")
public class ModelConfigController {

    private final ModelConfigService modelConfigService;

    /**
     * 获取所有启用的模型配置
     */
    @GetMapping("/list")
    @ApiOperation("获取所有模型配置")
    public R<List<ModelConfig>> getAllModels() {
        try {
            List<ModelConfig> models = modelConfigService.getEnabledModels();
            log.info("获取所有启用的模型配置，共{}个", models.size());
            return R.ok(models);
        } catch (Exception e) {
            log.error("获取所有模型配置失败", e);
            return R.error("获取模型配置失败: " + e.getMessage());
        }
    }

    /**
     * 获取Text2Img模型配置
     */
    @GetMapping("/text2img")
    @ApiOperation("获取Text2Img模型配置")
    public R<List<ModelConfig>> getText2ImgModels() {
        try {
            List<ModelConfig> models = modelConfigService.getText2ImgModels();
            log.info("获取Text2Img模型配置，共{}个", models.size());
            return R.ok(models);
        } catch (Exception e) {
            log.error("获取Text2Img模型配置失败", e);
            return R.error("获取Text2Img模型配置失败: " + e.getMessage());
        }
    }

    /**
     * 获取Img2Img模型配置
     */
    @GetMapping("/img2img")
    @ApiOperation("获取Img2Img模型配置")
    public R<List<ModelConfig>> getImg2ImgModels() {
        try {
            List<ModelConfig> models = modelConfigService.getImg2ImgModels();
            log.info("获取Img2Img模型配置，共{}个", models.size());
            return R.ok(models);
        } catch (Exception e) {
            log.error("获取Img2Img模型配置失败", e);
            return R.error("获取Img2Img模型配置失败: " + e.getMessage());
        }
    }

    /**
     * 根据模型名称获取模型配置
     */
    @GetMapping("/{modelName}")
    @ApiOperation("根据模型名称获取模型配置")
    public R<ModelConfig> getModelByName(@PathVariable String modelName) {
        try {
            ModelConfig model = modelConfigService.getModelConfig(modelName);
            if (model == null) {
                log.warn("模型配置不存在: {}", modelName);
                return R.error("模型配置不存在: " + modelName);
            }
            log.info("获取模型配置成功: {}", modelName);
            return R.ok(model);
        } catch (Exception e) {
            log.error("获取模型配置失败: {}", modelName, e);
            return R.error("获取模型配置失败: " + e.getMessage());
        }
    }

    /**
     * 获取默认模型配置
     */
    @GetMapping("/default")
    @ApiOperation("获取默认模型配置")
    public R<ModelConfig> getDefaultModel() {
        try {
            ModelConfig model = modelConfigService.getDefaultModel();
            log.info("获取默认模型配置成功: {}", model.getModelName());
            return R.ok(model);
        } catch (Exception e) {
            log.error("获取默认模型配置失败", e);
            return R.error("获取默认模型配置失败: " + e.getMessage());
        }
    }

    /**
     * 检查模型是否存在
     */
    @GetMapping("/exists/{modelName}")
    @ApiOperation("检查模型是否存在")
    public R<Boolean> checkModelExists(@PathVariable String modelName) {
        try {
            boolean exists = modelConfigService.existsModel(modelName);
            log.info("检查模型是否存在: {} = {}", modelName, exists);
            return R.ok(exists);
        } catch (Exception e) {
            log.error("检查模型是否存在失败: {}", modelName, e);
            return R.error("检查模型是否存在失败: " + e.getMessage());
        }
    }

} 